package com.xuanwei.admin.controller;


import com.xuanwei.core.response.ResultVO;
import com.xuanwei.core.service.IXwCategoryService;
import com.xuanwei.core.util.ParamUtil;
import com.xuanwei.dao.entity.XwCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author mjy
 * @since 2019-04-14
 */
@RestController
@RequestMapping("/api/category")
public class CategoryController extends BasicController {

    @Autowired
    private IXwCategoryService categoryService;

    @RequestMapping(value = "add.do", method = RequestMethod.POST)
    public ResultVO add(String name, String subName, String pic, Integer shortOrder, Integer show) {
        if (!ParamUtil.checkIsString(name)) {
            return ResultVO.failResult("类目名不能为空");
        }

        if (!ParamUtil.checkIsString(subName)) {
            return ResultVO.failResult("类目说明不能为空");
        }

        if (!ParamUtil.checkIsString(pic)) {
            return ResultVO.failResult("图片不能为空");
        }

        if (!ParamUtil.checkIsContainArray(show, 1, 0)) {
            show = 0;
        }
        categoryService.add(name, subName, pic, getId(), shortOrder, show);
        return ResultVO.successResult("添加成功");
    }

    @RequestMapping(value = "update.do", method = RequestMethod.POST)
    public ResultVO update(Integer id, String name, String subName, String pic, Integer shortOrder, Integer show) {
        if (!ParamUtil.checkIsPositiveInteger(id)) {
            return ResultVO.failResult("id参数格式错误");
        }
        if (!ParamUtil.checkIsString(name)) {
            return ResultVO.failResult("类目名不能为空");
        }

        if (!ParamUtil.checkIsString(subName)) {
            return ResultVO.failResult("类目说明不能为空");
        }

        if (!ParamUtil.checkIsString(pic)) {
            return ResultVO.failResult("图片不能为空");
        }
        categoryService.update(id, name, subName, pic, getId(), shortOrder, show);
        return ResultVO.successResult("修改成功");
    }

    @RequestMapping(value = "delete.do", method = RequestMethod.GET)
    public ResultVO delete(Integer id) {
        if (!ParamUtil.checkIsPositiveInteger(id)) {
            return ResultVO.failResult("id参数格式错误");
        }
        categoryService.delete(id);
        return ResultVO.successResult("删除成功");
    }

    @RequestMapping(value = "list.do", method = RequestMethod.GET)
    public ResultVO list() {
        return ResultVO.successResult(categoryService.all());
    }


}
